﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Web;
using System.Collections;
using System.Web.Caching;

namespace FastData.Caching
{
    public static class CacheProvider
    {
        public static object GetValue(string key)
        {
            CacheItem cacheItem = Get(key);
            if (cacheItem == null)
            {
                return null;
            }
            return cacheItem.Value;
        }
        
        public static CacheItem Get(string key)
        {
            return HttpRuntime.Cache[key] as CacheItem;
        }

        public static void Insert(string key, CacheItem item, DateTime absoluteExpiration)
        {
            Insert(key, item, absoluteExpiration, Cache.NoSlidingExpiration);
        }

        public static void Insert(string key, CacheItem item, TimeSpan slidingExpiration)
        {
            Insert(key, item, Cache.NoAbsoluteExpiration, slidingExpiration);
        }

        public static void Insert(string key, CacheItem item, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            HttpRuntime.Cache.Insert(key, item, null, absoluteExpiration, slidingExpiration);
        }
        
        public static void Remove(string key)
        {
            HttpRuntime.Cache.Remove(key);
        }

        public static void Remove(TableDependency relatedTable)
        {
            var cache = HttpRuntime.Cache;
            foreach (DictionaryEntry item in cache)
            {
                CacheItem cacheItem = item.Value as CacheItem;
                if (cacheItem != null && cacheItem.Dependencies.Contains(relatedTable))
                {
                    cache.Remove(item.Key.ToString());
                }
            }
        }
    }

}
